/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseCommentDao.cs
*        创建系统时间:       2012/11/2 19:06:59
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System;
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseCommentDao : BaseDao, IBaseDao
    {
        public BaseCommentDao()
        {
            base.CurrentTableName = BaseCommentTable.TableName;
        }

        public BaseCommentDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseCommentDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseCommentEntity commentEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseFolderTable.TableName);
            if (string.IsNullOrEmpty(commentEntity.ID))
            {
                commentEntity.ID = BaseSequenceDao.Instance.NewGuid();
            }
            commentEntity.SortCode = sequence;
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginInsert(BaseCommentTable.TableName);
            builder.SetValue(BaseCommentTable.FieldID, commentEntity.ID);
            builder.SetValue(BaseCommentTable.FieldOrganizeID, commentEntity.OrganizeID);
            builder.SetValue(BaseCommentTable.FieldFunctionID, commentEntity.FunctionID);
            builder.SetValue(BaseCommentTable.FieldCategoryID, commentEntity.CategoryID);
            builder.SetValue(BaseCommentTable.FieldObjectID, commentEntity.ObjectID);
            builder.SetValue(BaseCommentTable.FieldTitle, commentEntity.Title);
            builder.SetValue(BaseCommentTable.FieldContent, commentEntity.Content);
            builder.SetValue(BaseCommentTable.FieldWorked, Convert.ToInt32(commentEntity.Worked));
            builder.SetValue(BaseCommentTable.FieldPriorityID, commentEntity.PriorityID);
            builder.SetValue(BaseCommentTable.FieldImportant, Convert.ToInt32(commentEntity.Important));
            builder.SetValue(BaseCommentTable.FieldEnabled, Convert.ToInt32(commentEntity.Enabled));
            builder.SetValue(BaseCommentTable.FieldIPAddress, commentEntity.IPAddress);
            builder.SetValue(BaseCommentTable.FieldSortCode, commentEntity.SortCode);
            builder.SetValue(BaseCommentTable.FieldDescription, commentEntity.Description);
            builder.SetValue(BaseCommentTable.FieldCreateUserRealname, base.UserInfo.Realname);
            builder.SetValue(BaseCommentTable.FieldCreateUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseCommentTable.FieldCreateDate);
            if (builder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public string Add(string organizeID, string functionID, string categoryID, string objectID, string title, string content, bool worked, string priorityID, bool important, string ipAddress)
        {
            BaseCommentEntity commentEntity = new BaseCommentEntity
            {
                CreateUserID = base.UserInfo.ID,
                OrganizeID = organizeID,
                FunctionID = functionID,
                CategoryID = categoryID,
                ObjectID = objectID,
                Title = title,
                Content = content,
                Worked = worked,
                PriorityID = priorityID,
                Important = important,
                IPAddress = ipAddress,
                CreateUserRealname = base.UserInfo.Realname
            };
            return this.Add(commentEntity);
        }

        public int ChageWorked(IDbHelper dbHelper, string id, bool worked)
        {
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseCommentTable.TableName);
            builder.SetValue(BaseCommentTable.FieldWorked, worked ? 1 : -1);
            builder.SetWhere(BaseCommentTable.FieldID, id);
            return builder.EndUpdate();
        }

        public int ChangeWorked(string id)
        {
            string sqlQuery = " UPDATE " + BaseCommentTable.TableName + "    SET " + BaseCommentTable.FieldWorked + " = (CASE " + BaseCommentTable.FieldWorked + " WHEN 0 THEN 1 WHEN 1 THEN 0 END)  WHERE (" + BaseCommentTable.FieldID + " = ? )";
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            targetFileds[0] = BaseCommentTable.FieldID;
            targetValues[0] = id;
            return base.DbHelper.ExecuteNonQuery(sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
        }

        public DataTable GetDTByCategory(string categoryID, int topLimit)
        {
            return this.GetDT(BaseCommentTable.FieldFunctionID, "Comment", BaseCommentTable.FieldCategoryID, categoryID, BaseCommentTable.FieldCreateDate + " DESC ", topLimit);
        }

        public DataTable GetDTByCategory(string categoryID, string objectID)
        {
            return this.GetDT(BaseCommentTable.FieldCategoryID, categoryID, BaseCommentTable.FieldObjectID, objectID, BaseCommentTable.FieldSortCode + " DESC ");
        }

        public DataTable GetPreviousNextID(string id)
        {
            string sqlQuery = "   SELECT ID      FROM " + BaseCommentTable.TableName + "    WHERE (" + BaseCommentTable.FieldCreateUserID + " = ?)  ORDER BY " + BaseCommentTable.FieldSortCode;
            string[] targetFileds = new string[1];
            object[] targetValues = new object[1];
            targetFileds[0] = BaseCommentTable.FieldCreateUserID;
            targetValues[0] = base.UserInfo.ID;
            DataTable dataTable = new DataTable(BaseCommentTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public DataTable Search(string search)
        {
            if (search.Length == 0)
            {
                return this.GetDT();
            }
            DataTable dataTable = new DataTable(BaseCommentTable.TableName);
            string sqlQuery = "   SELECT *          ,(SELECT TOP 1 Fullname FROM " + BaseStaffTable.TableName + " WHERE ID = A.CreateUserID) AS CreateUserFullName          ,(SELECT TOP 1 Fullname FROM " + BaseItemDetailsTable.TableName + " WHERE Code = A.CategoryID) AS CategoryFullName          ,(SELECT TOP 1 PreviousVisit FROM " + BaseUserTable.TableName + " WHERE ID = A.CreateUserID) AS PreviousVisit      FROM " + BaseCommentTable.TableName + " AS A     WHERE (" + BaseCommentTable.FieldContent + " LIKE ? )        OR ((SELECT TOP 1 Fullname FROM " + BaseStaffTable.TableName + " WHERE ID = A.CreateUserID) LIKE ?)        OR ((SELECT TOP 1 Fullname FROM " + BaseItemDetailsTable.TableName + " WHERE ID = A.CategoryID) LIKE ?)        OR (CONVERT (NVARCHAR(10), " + BaseCommentTable.FieldCreateDate + ", 20) LIKE ? )  ORDER BY " + BaseCommentTable.FieldSortCode;
            string[] targetFileds = new string[4];
            object[] targetValues = new object[4];
            for (int i = 0; i < 4; i++)
            {
                targetFileds[i] = BaseCommentTable.FieldContent;
                targetValues[i] = search;
            }
            base.DbHelper.Fill(dataTable, sqlQuery, base.DbHelper.MakeParameters(targetFileds, targetValues));
            return dataTable;
        }

        public int SetWorked(string[] ids, int worked)
        {
            int num = 0;
            string id = string.Empty;
            try
            {
                base.DbHelper.BeginTransaction();
                for (int i = 0; i < ids.Length; i++)
                {
                    id = ids[i];
                    num += this.SetProperty(id, BaseCommentTable.FieldWorked, worked.ToString());
                }
                base.DbHelper.CommitTransaction();
            }
            catch (Exception exception)
            {
                base.DbHelper.RollbackTransaction();
                BaseExceptionDao.LogException(base.DbHelper, base.UserInfo, exception);
                throw exception;
            }
            return num;
        }

        public int Update(BaseCommentEntity commentEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseCommentTable.TableName);
            SQLBuilder builder = new SQLBuilder(base.DbHelper);
            builder.BeginUpdate(BaseCommentTable.TableName);
            builder.SetValue(BaseCommentTable.FieldCategoryID, commentEntity.CategoryID);
            builder.SetValue(BaseCommentTable.FieldTitle, commentEntity.Title);
            builder.SetValue(BaseCommentTable.FieldContent, commentEntity.Content);
            builder.SetValue(BaseCommentTable.FieldWorked, commentEntity.Worked);
            builder.SetValue(BaseCommentTable.FieldPriorityID, commentEntity.PriorityID);
            builder.SetValue(BaseCommentTable.FieldImportant, commentEntity.Important);
            builder.SetValue(BaseCommentTable.FieldSortCode, sequence);
            builder.SetValue(BaseCommentTable.FieldModifyUserID, base.UserInfo.ID);
            builder.SetDBNow(BaseCommentTable.FieldModifyDate);
            builder.SetWhere(BaseCommentTable.FieldID, commentEntity.ID);
            return builder.EndUpdate();
        }

        public int Update(string id, string categoryID, string title, string content, bool worked, string priorityID, bool important)
        {
            BaseCommentEntity commentEntity = new BaseCommentEntity
            {
                ID = id,
                ModifyUserID = base.UserInfo.ID,
                CategoryID = categoryID,
                Title = title,
                Content = content,
                Worked = worked,
                PriorityID = priorityID,
                Important = important
            };
            return this.Update(commentEntity);
        }
    }
}